package com.lmpop.trade.ti_state_history;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.RandomID;
import java.util.Hashtable;
import java.util.List;

public class Ti_state_historyInfo
  implements TradeInterf
{
  QueryList queryList;

  public Ti_state_historyInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    RandomID id = new RandomID();
    Ti_state_history ti_state_history = new Ti_state_history();

    String pkid_string = map.getString("pkid");
    ti_state_history.setState_code(map.getString("up_operating"));
    String s_start_date = map.getString("s_start_date");
    String s_end_date = map.getString("s_end_date");

    if (!s_start_date.equals(""))
      ti_state_history.setStart_date(s_start_date);
    else {
      ti_state_history.setStart_date("2000-01-01 00:00:00");
    }
    if (!s_end_date.equals(""))
      ti_state_history.setEnd_date(s_end_date);
    else {
      ti_state_history.setEnd_date("2050-01-01 00:00:00");
    }

    ti_state_history.setUser_id(map.getString("session_user_id"));
    ti_state_history.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (!pkStr[i].equals("")) {
            ti_state_history.setTrade_id(id.GenTradeId());
            ti_state_history.setInfo_id(pkStr[i]);
            insert(dbmMgr, ti_state_history);
          }
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_state_history ti_state_history)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_state_history", ti_state_history);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_state_history where trade_id=#value#
    dbmMgr.update("deleteTi_state_history", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_state_history ti_state_history = new Ti_state_history();
    ti_state_history.setTrade_id(map.getString("trade_id"));
    ti_state_history.setInfo_id(map.getString("info_id"));
    ti_state_history.setState_code(map.getString("state_code"));
    ti_state_history.setStart_date(map.getString("start_date"));
    ti_state_history.setEnd_date(map.getString("end_date"));
    ti_state_history.setUser_id(map.getString("user_id"));
    ti_state_history.setIn_date(map.getString("in_date"));
    ti_state_history.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_state_history);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_state_history ti_state_history)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_state_history", ti_state_history);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_state_history ti_state_history = (Ti_state_history)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_state_history.setStart(Integer.valueOf(start));
    ti_state_history.setLimit(Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_state_historyPage", ti_state_history);
  }

  public int getCountByObj(Object obj)
  {
    Ti_state_history ti_state_history = (Ti_state_history)obj;
    List list = this.queryList.getList("getCountByObjTi_state_history", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_state_historyByPk", pk);
  }
}